package com.fenye.testflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fenye.testflow.mapper.SysRoleMapper;
import com.fenye.testflow.model.common.Result;
import com.fenye.testflow.model.common.ResultCodeEnum;
import com.fenye.testflow.model.dto.SysRoleDto;
import com.fenye.testflow.model.entity.system.SysRole;
import com.fenye.testflow.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Override
    public Result<SysRole> findPage(Long pageNum, Long pageSize, SysRoleDto sysRoleDto) {
        Page<SysRole> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();

        if (sysRoleDto.getRoleName() != null){
            queryWrapper.like(SysRole::getRoleName, sysRoleDto.getRoleName());
        }
        Page<SysRole> pageData = sysRoleMapper.selectPage(page, queryWrapper);
        System.out.println(pageData.getRecords());

        return Result.build(pageData, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Map<String, Object> findAllRoles() {

        List<SysRole> allSysRoles = sysRoleMapper.selectList(null);
        Map<String, Object> rolesMap = new HashMap<>();
        rolesMap.put("allRoles", allSysRoles);
        return rolesMap;
    }
}
